/**
 * 定时任务 - 编辑页面
 * @author yuan_rz
 * @date 2019/03/11 18:03
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        lastFiveTimes = function (rule, value, callback) {
            if(value == null || value == ''){
                callback();
            }else{
                app.ajax({
                    xurl: 'sys/task/lastFiveTimes',
                    xdata: {
                        cron : value
                    },
                    loading: false,
                    cb: function (result) {
                        if (result.ok) {
                            if(result.data.data instanceof Array){
                                vueData.dateList = result.data.data;
                                callback();
                            }else{
                                callback(new Error(result.data.data));
                            }
                        }
                    }
                });
            }
        },
        vueData = {
            isAdd: false,
            dateList: [],
            form: '',
            rules: {
                TASK_NAME: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                TASK_CLASS: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                TASK_RULE: [
                    {required: true, message: '不可为空', trigger: 'change'},
                    {validator: lastFiveTimes, trigger: 'change'}
                ],
                TASK_GROUP: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                TASK001: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ]
            },
            selectOptions: {
                sys_task_group : app.datas_('sys_task_group'),
                classList: [],
                loadList: []
            }
        },
        callObj
    ;

    var taskEdit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'task_edit',
                _loadTplsFull: [{
                    dom: 'task_rule', tpl: 'system/task_rule'
                }]
            }, win);
        },
        initData: function(){
            self.getClassList();
            self.getLoadList();
        },
        getClassList: function(){
            app.ajax({
                xurl: 'sys/task/getClassList',
                xdata: {},
                cb: function (result) {
                    if (result.ok) {
                        vueData.selectOptions.classList = result.data.data;
                    }
                }
            });
        },
        getLoadList: function(){
            app.ajax({
                xurl: 'sys/load/getLoadList',
                xdata: {},
                cb: function (result) {
                    if (result.ok) {
                        vueData.selectOptions.loadList = result.data.data;
                    }
                }
            });
        },
        editData: function () {
            app.ajax({
                xurl: 'sys/task/editData',
                xdata: vueData.form,
                loading: true,
                okmsg: self._moduleName + '信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('task').reloadTable();
                        self._closeTpl();
                    }
                }
            });
        },
        addData: function () {
            app.ajax({
                xurl: 'sys/task/addData',
                xdata: vueData.form,
                loading: true,
                okmsg: self._moduleName + '信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('task').reloadTable();
                        self._closeTpl();
                    }
                }
            });
        },
        _call: function (tagData) {
            if (tagData.is_add) {
                vueData.isAdd = true;
                app.extendUpLow({
                    target: vueData.form,
                    res: {
                        TASK_STATUS: '0',
                        TASK004: 0
                    },
                    reset: true
                });
            } else {
                vueData.isAdd = false;
                callObj = tagData.obj;
                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.data,
                    reset: true
                });
            }

        },
        _ready: function () {
            vueData.form = app.fields_(['sys_task']);
            var vue = new Vue({
                el: '#task_edit',
                data: vueData,
                watch: {
                    'form.TASK_GROUP': function(newValue, oldValue){
                        if(newValue == '1'){
                            self.getLoadList();
                        }
                    }
                },
                methods: {
                    openTaskRule: function () {
                        self._openTplFull({
                            tag: 'task_rule',
                            tagData: {
                                cb: function (cron) {
                                    vueData.form.TASK_RULE = cron;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '设置任务规则',
                                area: ['500px', '360px']
                            }
                        });
                    },
                    cancel: function () {
                        self._closeTpl();
                    },
                    sure: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                if (vueData.isAdd) {
                                    self.addData();
                                } else {
                                    self.editData();
                                }
                            }
                        });
                    },
                    taskClassChange:function () {
                        vueData.selectOptions.classList.iFilter({
                            o:{code: vueData.form.TASK_CLASS},
                            cb:function (arr) {
                                vueData.form.TASK_NAME = arr[0].value;
                            }
                        });
                    }
                },
                created: function () {
                    self._setEleIsReady('task_edit');
                }
            });
            self.initData();
        },
        _lazy: function () {
            this._super();
        }
    });

    return new taskEdit();

});